package com.ning.farm_income_back.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ning.farm_income_back.domain.Costs;
import com.ning.farm_income_back.domain.Land;
import com.ning.farm_income_back.domain.Users;
import com.ning.farm_income_back.dto.AgriculturalassetsDto;
import com.ning.farm_income_back.dto.CostsDto;
import com.ning.farm_income_back.mapper.CostsMapper;
import com.ning.farm_income_back.mapper.LandMapper;
import com.ning.farm_income_back.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CostsService {
    @Autowired
    private CostsMapper costsMapper;

    @Autowired
    private LandMapper landMapper;

    @Autowired
    private UsersMapper usersMapper;


    public PageInfo getPage(CostsDto costDto, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize,"cost_id desc ");


        List<CostsDto> list =  costsMapper.selectBy(costDto);

        return new PageInfo<>(list);
    }

    public void saveCostInfo(CostsDto costsDto, String username) {
        Land l = landMapper.selectByUsed(costsDto.getUsed());
        Users u = usersMapper.selectByUsername(username);
        Costs costs = costsDto;
        costs.setLandId(l.getLandId());
        costs.setRecorderId(u.getUserId());
        Date date = new Date();
        costs.setCostDate(date);
        costsMapper.insertSelective(costs);
    }

    public void updateCostInfo(CostsDto costsDto, String username) {
        Land l = landMapper.selectByUsed(costsDto.getUsed());
        Users u = usersMapper.selectByUsername(username);
        Costs costs = costsDto;
        costs.setLandId(l.getLandId());
        costs.setRecorderId(u.getUserId());
        Date date = new Date();
        costs.setCostDate(date);
        costsMapper.updateByPrimaryKey(costs);
    }

    //删除服务
    public void deleteCostInfo(Integer id) {
        costsMapper.deleteByPrimaryKey(id.longValue());
    }


    public List<String> getCostsEveryYear(String startY, String endY) {
        //给定起始年，最终年，根据年份不同，计算每一年的总价之和。以列表形式返回。
        //每一年的收益
        return costsMapper.selectEveryYear(startY,endY);

    }

    public void updateCostStatus(Costs costs) {
        costs.setReviewDate(new Date());
        costsMapper.updateByPrimaryKey(costs);
    }

    public void rejectCost(Costs costs) {
        costs.setReviewDate(new Date());
        costsMapper.updateByPrimaryKey(costs);
    }
}
